package com.chinafree.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinafree.model.po.ChUserCompany;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface ChUserCompanyMapper extends BaseMapper<ChUserCompany> {

    @Select("SELECT a.id,a.companyId"
            + "FROM" +
            "\tch_user_company a\n" +
            "INNER JOIN (\n" +
            "\tSELECT\n" +
            "    companyId\t,\n" +
            "\t\tMAX(id) AS max_id\n" +
            "\tFROM\n" +
            "\t\tch_user_company\n" +
            "\tGROUP BY\n" +
            "\t\tcompanyId \n" +
            ") b ON a.companyId = b.companyId\n" +
            "AND a.id = b.max_id\n" +
            "where a.userId = #{userId}")
    void selectCompany();
 
    @Select({
        "SELECT user_id from ch_user_company "
                + "where company_id = #{companyId} GROUP BY user_id "
                + "ORDER BY  max(create_time) DESC" })
    List<Long> getCompanyList(@Param("companyId") Long companyId);

    @Select({
        "SELECT  company_id from ch_user_company "
                + "where user_id = #{userId} GROUP BY company_id "
                + "ORDER BY  max(create_time) DESC" })
    List<Long> getUserList(@Param("userId")Long userId);

    
}